package com.qingxin.qrCode.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.qingxin.park.entity.Company;
import com.qingxin.qrCode.entity.QRCode;
import com.qingxin.qrCode.service.QRCodeService;
import com.qingxin.qrCode.service.QrNumService;
import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.ui.bean.TreeNode;
@Controller
@RequestMapping("/qrCode")
public class QRCodeAction extends BaseAction {

	@Autowired
	private QRCodeService qrCodeService;
	@Autowired
	private QrNumService  qrNumService;
	
	/**
	 * 二维码数据
	 * @param pageParam
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/listdata.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object listdata(String leftdata,String type, PageParam pageParam, HttpServletRequest request){
		return qrCodeService.list(leftdata, type,pageParam, getLoginUser(request));
	}
	
	@RequestMapping(value="/qrNumlistdata.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object qrnumlistdata(String codeid, PageParam pageParam, HttpServletRequest request){
		return qrCodeService.qrnumlist(codeid, pageParam, getLoginUser(request));
	}
	
	@RequestMapping(value="/list.do", method=RequestMethod.GET)
	public String list(ModelMap model,@RequestParam  String type,HttpServletRequest request){
		model.put("type", type);
		return "trace/qrCode/qrCode_list";
	}
	/**
	 * 添加
	 * @param model
	 * @param leftdata
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/add.do", method=RequestMethod.GET)
	public String add(ModelMap model,String type, String leftdata, HttpServletRequest request){
		model.put("parkid", leftdata);
		model.put("type", type);
		return "/trace/qrCode/qrCode_add";
	}
	
	
	@RequestMapping(value="/showQrNum.do", method=RequestMethod.GET)
	public String showQrNum(ModelMap model,String codeid, HttpServletRequest request){
		model.put("codeid", codeid);
		return "/trace/qrCode/qrNum_list";
	}
	
	@RequestMapping(value="/deleteqrnum.json", method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object deleteqrnum(String[] qrnumid, HttpServletRequest request){
		if(qrnumid==null || qrnumid.length==0){
			return new SysError("没有选择任何信息.");
		}
		return qrNumService.delete(qrnumid, getLoginUser(request), request);
	}
	
	
	/**
	 * 保存
	 * @param qrCode
	 * @param number
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/save.json", method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object save(@Valid QRCode qrCode,BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		SysError info = qrCodeService.add(qrCode,getLoginUser(request), request);
		return info;
	}
	
	@RequestMapping(value="/delete.json", method=RequestMethod.POST)
	@ResponseBody
	public Object delete(String codeid, HttpServletRequest request){
		if(codeid==null ){
			return new SysError("没有选择任何信息.");
		}
		return qrCodeService.delete(codeid, getLoginUser(request), request);
	}
	
	/**
	 * 企业下拉列表数据
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getCompanyData", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public List<Company> getParkData(HttpServletRequest request){
		return qrCodeService.getParkData(getLoginUser(request));
	}
	
	/**
	 * 获取农作物数据
	 * @param parkid
	 * @param pid
	 * @return
	 */
	@RequestMapping(value="/getCropData.json" , method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object getCropData(String parkid,String type){
		List<TreeNode> list = this.qrCodeService.getUITreeDataType(parkid, type, true);
		for(TreeNode node : list){
			node.setChildren(this.qrCodeService.getUITreeDataProduct(parkid, type,node.getId(), false));
		}
		return list;
	}

}
